<template>
  <div class="page-container">
    <!-- 导航栏 -->
    <div class="navbar">
      <div class="navbar-content">
        <div class="navbar-left">
          <button @click="$router.back()" class="back-btn">
            <i class="fas fa-arrow-left"></i>
          </button>
          <h1 class="page-title">观看历史</h1>
        </div>
        <button class="clear-btn" @click="clearHistory">清空</button>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="content-area">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
        <p class="loading-text">正在加载数据...</p>
      </div>

      <!-- 历史记录列表 -->
      <div v-else-if="watchHistory.length > 0" class="history-list">
        <div class="history-item" v-for="item in watchHistory" :key="item.id" @click="toPlayer(item.id)">
          <div class="item-content">
            <div class="cover-container">
              <img :src="item.cover" class="cover-img" :alt="item.title" />
              <div class="duration-badge">05:32</div>
            </div>
            <div class="item-info">
              <h4 class="item-title">{{ item.title }}</h4>
              <p class="item-progress">已观看至第{{ item.currentWatchEpisode }}集 · 全{{ item.totalEpisodes }}集</p>
              <div class="item-footer">
                <span class="watch-time">2小时前</span>
                <button class="delete-btn" @click.stop="deleteHistoryItem(item.id)">删除</button>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-else class="empty-state">
        <i class="far fa-clock empty-icon"></i>
        <h3 class="empty-title">暂无观看历史</h3>
        <p class="empty-desc">开始观看精彩内容吧</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reqBrowsingHistory, reqDeleteBrowsingRecord, reqBatchDeleteBrowsingRecords } from '@/apis/video'
const router = useRouter()

// 浏览记录数据
const watchHistory = ref([])
// 加载状态
const loading = ref(true)

// 跳转到播放器页面
const toPlayer = (id) => {
  router.push({ name: 'Player', params: { dramaId: id } })
}

// 清空浏览记录
const clearHistory = () => {
  showConfirmDialog({
    title: '标题',
    message: '确定要清空观看历史吗？'
  })
    .then(async () => {
      const ids = watchHistory.value.map((item) => item.id)
      const response = await reqBatchDeleteBrowsingRecords(ids)
      console.log('清空观看历史响应:', response)
      watchHistory.value = []

      showToast('已清空观看历史')
    })
    .catch(() => {
      // on cancel
    })
}

// 删除单个历史记录
const deleteHistoryItem = (id) => {
  const index = watchHistory.value.findIndex((item) => item.id === id)
  if (index > -1) {
    const deletedItem = watchHistory.value[index]

    showConfirmDialog({
      title: '标题',
      message: '确定要删除该观看记录吗？'
    })
      .then(async () => {
        const response = await reqDeleteBrowsingRecord(id)
        console.log('清空观看历史响应:', response)

        watchHistory.value.splice(index, 1)

        showToast(`已删除《${deletedItem.title}》`)
      })
      .catch(() => {
        // on cancel
      })
  }
}

// 获取浏览记录
const getHistory = async () => {
  try {
    loading.value = true
    // 获取浏览记录
    const { data: historys } = await reqBrowsingHistory()
    watchHistory.value = historys.list || []
    if (!historys.list || historys.list.length === 0) {
      showToast('暂无观看历史')
    }
  } catch (error) {
    showToast('获取数据失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  getHistory()
})
</script>

<style scoped lang="scss">
.page-container {
  width: 100%;
  min-height: 100vh;
  background-color: #f9fafb;
  position: relative;
}

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background-color: white;
  border-bottom: 1px solid #f3f4f6;

  .navbar-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;

    .navbar-left {
      display: flex;
      align-items: center;

      .back-btn {
        margin-right: 16px;
        color: #4b5563;
        background: none;
        border: none;
        cursor: pointer;
        padding: 8px;
      }

      .back-btn:hover {
        color: #374151;
      }

      .page-title {
        font-size: 18px;
        font-weight: 600;
        color: #1f2937;
        margin: 0;
      }
    }

    .clear-btn {
      color: #2563eb;
      font-size: 14px;
      font-weight: 500;
      background: none;
      border: none;
      cursor: pointer;
      padding: 8px;
    }

    .clear-btn:hover {
      color: #1d4ed8;
    }
  }
}

.content-area {
  padding: 24px 16px 80px;
  padding-top: 80px; /* 为固定导航栏留出空间 */

  .loading-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 3rem 0;
    height: 30vh;
  }

  .loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(0, 0, 0, 0.1);
    border-top-color: #f97316;
    border-radius: 50%;
    animation: spin 1s ease-in-out infinite;
    margin-bottom: 1rem;
  }

  .loading-text {
    color: #666;
    font-size: 1rem;
  }

  @keyframes spin {
    to {
      transform: rotate(360deg);
    }
  }

  .history-list {
    display: flex;
    flex-direction: column;
    gap: 16px;

    .history-item {
      background-color: white;
      border-radius: 8px;
      box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
      overflow: hidden;

      .item-content {
        display: flex;
        padding: 16px;

        .cover-container {
          width: 80px;
          height: 96px;
          background: linear-gradient(135deg, #3b82f6 0%, #9333ea 100%);
          border-radius: 8px;
          margin-right: 12px;
          flex-shrink: 0;
          position: relative;
          overflow: hidden;

          .cover-img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }

          .duration-badge {
            position: absolute;
            bottom: 4px;
            right: 4px;
            background-color: rgba(0, 0, 0, 0.6);
            color: white;
            font-size: 12px;
            padding: 2px 4px;
            border-radius: 4px;
          }
        }

        .item-info {
          flex: 1;
          min-width: 0;

          .item-title {
            font-weight: 500;
            color: #1f2937;
            margin: 0 0 4px 0;
          }

          .item-progress {
            font-size: 14px;
            color: #4b5563;
            margin: 0 0 8px 0;
          }

          .item-footer {
            display: flex;
            align-items: center;
            justify-content: space-between;

            .watch-time {
              font-size: 14px;
              color: #6b7280;
            }

            .delete-btn {
              color: #dc2626;
              font-size: 14px;
              background: none;
              border: none;
              cursor: pointer;
              padding: 4px;
            }

            .delete-btn:hover {
              color: #b91c1c;
            }
          }
        }
      }
    }
  }

  .empty-state {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 3rem 0;
    height: 30vh;
    text-align: center;

    .empty-icon {
      color: #d1d5db;
      font-size: 3rem;
      margin-bottom: 1rem;
    }

    .empty-title {
      color: #6b7280;
      font-weight: 500;
      margin: 0 0 8px 0;
    }

    .empty-desc {
      color: #9ca3af;
      font-size: 14px;
      margin: 0;
    }
  }

  .toast-message {
    position: fixed;
    bottom: 5rem;
    left: 50%;
    transform: translateX(-50%);
    background-color: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    z-index: 50;
  }
}
</style>
